﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LayerEntity.Sistema;
using LayerEntity.Persona;
using System2012.Persona;
using System.Collections;

namespace System2012.Listados
{
    public partial class frmListVentasClientes : DevExpress.XtraEditors.XtraForm
    {
        private int id_Dicliente;
        public   DataTable dtbCabFact;
        public int Row;
        public frmListVentasClientes()
        {
            InitializeComponent();
           
        }
        public frmListVentasClientes(ArrayList data)
        {
            InitializeComponent();

            this.txtIdCliente.Text = data[0].ToString();
            txtCliente.Text = data[1].ToString();
            txtDireccion.Text =data[2].ToString();
            txtDocCliente.Text = data[3].ToString();
            if (!BGWListCab.IsBusy) BGWListCab.RunWorkerAsync();
        }
        void SeleccionarCliente()
        {
            Persona.BuscarPersona per = new BuscarPersona(EnumTipoPersona.Cliente);
            per.ShowDialog();
            if (per.Estado)
            {
                IEPersona cli = per.Persona;

                this.txtIdCliente.EditValue = cli.IdPersona;
                this.txtCliente.EditValue = cli.RazonSocial;
                this.txtDocCliente.EditValue = cli.NumeroDocumento;
                EDireccion dir = cli.GetAddress(per.IdAddressSelected);
                id_Dicliente = dir.Item;//
                this.txtDireccion.EditValue = dir.Direccion;
                if (!BGWListCab.IsBusy) BGWListCab.RunWorkerAsync();
            }
            per.Dispose();
        }
        private void SelectDocument()
        {
            if (Row >= 0)
            {
                this.Close();
            }
        }
        private void BGWListCab_DoWork(object sender, DoWorkEventArgs e)
        {
            ESistema obj = new ESistema();
            StringBuilder cadena = new StringBuilder();
            cadena.AppendFormat(" where id_cliente='{0}'", txtIdCliente.Text);
            obj.Condicion = cadena.ToString();
            e.Result = System2012.SystemUtils.RecordManagerFacturacion.GetCabeceraFacturacionVentas(obj);
        }

        private void BGWListCab_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dtbCabFact=(DataTable)e.Result;
            gridControl1.DataSource = e.Result;
        }

        private void txtIdCliente_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                this.SeleccionarCliente();
            }
        }

        private void txtCliente_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            SeleccionarCliente();
        }

        private void BGWDetFact_DoWork(object sender, DoWorkEventArgs e)
        {
            ESistema obj = new ESistema();
            StringBuilder cadena = new StringBuilder();
            if (Row >= 0)
            {
                cadena.AppendFormat(" where serie_facturacion ='{0}'", dtbCabFact.Rows[Row]["serie_facturacion"]);
                cadena.AppendFormat("and numdoc_facturacion='{0}'", dtbCabFact.Rows[Row]["numdoc_facturacion"]);
                cadena.AppendFormat("and id_sucursal={0}", dtbCabFact.Rows[Row]["id_sucursal"]);
                cadena.AppendFormat("and id_documento={0}", dtbCabFact.Rows[Row]["id_documento"]);
                cadena.AppendFormat("and id_dir_cliente={0}", dtbCabFact.Rows[Row]["id_dir_cliente"]);
                cadena.AppendFormat("and id_cliente='{0}'", dtbCabFact.Rows[Row]["id_cliente"]);
                cadena.AppendFormat("and fecha_facturacion ='{0}'", dtbCabFact.Rows[Row]["fecha_facturacion"]);
                obj.Condicion = cadena.ToString();
                e.Result = System2012.SystemUtils.RecordManagerFacturacion.GetDetaFacturacionVentas(obj);
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            Row = e.FocusedRowHandle;
            if (!BGWDetFact.IsBusy) BGWDetFact.RunWorkerAsync();
        }

        private void BGWDetFact_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            gridControl2.DataSource = e.Result;
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (!BGWDetFact.IsBusy) BGWDetFact.RunWorkerAsync();
        }

        private void gridControl1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                SelectDocument();
            }
        }

        private void frmListVentasClientes_Load(object sender, EventArgs e)
        {

        }
    }
}